package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.entity.Article;
import com.sangeng.entity.Category;
import com.sangeng.entity.ResponseResult;
import com.sangeng.mapper.UserDao;
import com.sangeng.entity.User;
import com.sangeng.service.UserService;
import com.sangeng.util.BeanCopyUtils;
import com.sangeng.util.SecurityUtils;
import com.sangeng.vo.CategoryVo;
import com.sangeng.vo.PageVo;
import com.sangeng.vo.UserInfoVo;
import com.sangeng.vo.UserVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2022-08-20 14:02:29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Override
    public ResponseResult getUserInfo() {

        //获取当前用户·id，不能从前端传回来，因为有些人如果伪造请求就会泄露用户信息
        Long userId = SecurityUtils.getUserId();
        //根据用户id查询用户信息
        User user = getById(userId);
        //下面这种写法因为是从redis中取的，并不能及时更新
//        LoginUserDetailsImpl user = SecurityUtils.getLoginUser();
//        //copy到vo
//        User user1 = user.getUser();

        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);

        return  ResponseResult.okResult(userInfoVo);
    }

    @Override
    public ResponseResult updateUserInfo(User user) {
        boolean b = updateById(user);

        return ResponseResult.okResult(b);
    }
//
//    @Override
//    public ResponseResult getAllUserInfo(Integer pageNum, Integer pageSize) {
//        Page<User> userPage = new Page<User>(pageNum,pageSize);
////查询所有未被删除的category
//        Page<User> page = page(userPage, null);
//        List<User> pageList = page.getRecords();
//        List<UserVO> userVos = BeanCopyUtils.copyBean(pageList, UserVO.class);
//        long total = page.getTotal();
//        PageVo pageVo = new PageVo(userVos, total);
//        return ResponseResult.okResult(pageVo);
//
//    }

    @Override
    public ResponseResult getUserList(Integer pageNum, Integer pageSize, String name, String status, String phonenumber) {
        Page<User> userPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //模糊匹配，如果有title，那么就把title的查询条件加上
        userLambdaQueryWrapper
                .like(StringUtils.hasText(name),User::getUserName,name)
                .eq(StringUtils.hasText(phonenumber),User::getPhonenumber,phonenumber)
        .eq(StringUtils.hasText(status),User::getStatus,status);

        // articleLambdaQueryWrapper.like();
        Page<User> page = page(userPage, userLambdaQueryWrapper);
        List<User> userList= page.getRecords();
        long total = page.getTotal();
        PageVo pageVo = new PageVo(userList, total);
        return ResponseResult.okResult(pageVo);
    }
}

